# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Contributor: Ariadne Conill <ariadne@dereferenced.org>
# Maintainer: Drew DeVault <sir@cmpwn.com>
pkgname=binutils-cross
pkgver=2.38
pkgrel=0
pkgdesc="Tools necessary to build programs for foreign architectures"
url="https://www.gnu.org/software/binutils/"
makedepends_build="bison flex texinfo"
makedepends_host="zlib-dev"
makedepends="$makedepends_build $makedepends_host"
arch="all"
license="GPL-2.0 GPL-3.0-or-later LGPL-2.0 BSD"
source="https://ftp.gnu.org/gnu/binutils/binutils-$pkgver.tar.xz
	binutils-ld-fix-static-linking.patch
	gold-mips.patch
	ld-bfd-mips.patch
	0001-Revert-PR25882-.gnu.attributes-are-not-checked-for-s.patch
	binutils-mips-disable-assert.patch
	binutils-ppc-fix-machine-options.patch
	binutils-s390x-1.patch
	binutils-s390x-2.patch
	"
builddir="$srcdir/binutils-$pkgver"
targets="aarch64 armhf armv7 mips64 ppc64le riscv64 s390x x86 x86_64"
options="!check"
for target in $targets
do
	subpackages="$subpackages binutils-$target:_$target"
done

build() {
	for target in $targets
	do
		local _arch_configure=""
		if [ "$target" = "x86_64" ]; then
			_arch_configure="--enable-targets=x86_64-pep"
		fi

		case "$target" in
		mips*)  _hash_style_configure="--enable-default-hash-style=sysv" ;;
		*)      _hash_style_configure="--enable-default-hash-style=gnu"	;;
		esac

		mkdir "$srcdir/$target"
		cd "$srcdir/$target"

		local ctarget="$(arch_to_hostspec "$target")"
		"$builddir"/configure \
			--build=$CBUILD \
			--host=$CHOST \
			--target=$ctarget \
			--program-prefix=$ctarget- \
			--prefix=/usr \
			--mandir=/usr/share/man \
			--infodir=/usr/share/info \
			--disable-multilib \
			--enable-ld=default \
			--disable-gold \
			--enable-64-bit-bfd \
			--disable-plugins \
			--enable-relro \
			--enable-deterministic-archives \
			--disable-internal-libiberty \
			$_arch_configure \
			$_hash_style_configure \
			--with-pic \
			--disable-werror \
			--disable-nls \
			--with-mmap \
			--with-system-zlib
		make
	done
}

package() {
	pkgdesc="Tools necessary to build programs for foreign architectures"
	for target in $targets
	do
		depends="$depends binutils-$target"
	done
	mkdir -p "$pkgdir"
}

_install_target() {
	local target="$1"
	pkgdesc="Tools necessary to build programs for $target"
	cd "$srcdir/$target"
	make install DESTDIR="$subpkgdir"
	rm -rf "$subpkgdir"/usr/share
	rm -rf "$subpkgdir"/usr/lib
	rm -rf "$subpkgdir"/usr/include
}

_aarch64() {
	_install_target aarch64
}

_armhf() {
	_install_target armhf
}

_armv7() {
	_install_target armv7
}

_mips64() {
	_install_target mips64
}

_ppc64le() {
	_install_target ppc64le
}

_riscv64() {
	_install_target riscv64
}

_s390x() {
	_install_target s390x
}

_x86() {
	_install_target x86
}

_x86_64() {
	_install_target x86_64
}

sha512sums="
8bf0b0d193c9c010e0518ee2b2e5a830898af206510992483b427477ed178396cd210235e85fd7bd99a96fc6d5eedbeccbd48317a10f752b7336ada8b2bb826d  binutils-2.38.tar.xz
ecee33b0e435aa704af1c334e560f201638ff79e199aa11ed78a72f7c9b46f85fbb227af5748e735fd681d1965fcc42ac81b0c8824e540430ce0c706c81e8b49  binutils-ld-fix-static-linking.patch
f55cf2e0bf82f97583a1abe10710e4013ecf7d64f1da2ef8659a44a06d0dd8beaf58dab98a183488ea137f03e32d62efc878d95f018f836f8cec870bc448556f  gold-mips.patch
314d2ef9071c89940aa6c8118e8a1e2f191a5d0a4bf596da1ad9cc84f884d8bc7dea8bd7b9fc3f8f1bddd3fd41c6eb017e1e804044b3bf084df1ed9e6e095e2d  ld-bfd-mips.patch
70ec22bd72ef6dddecfd970613387dd4a8cdc8730dd3cbf03d5a0c3a7c4d839383167bb06dad21bf7c235329fd44b5dc4aefe762f68544f17155cf002bf1be4a  0001-Revert-PR25882-.gnu.attributes-are-not-checked-for-s.patch
609cd90d8b334eb309f586b17b9d335a08d3dbb6def7c3eb5c010028fcb681674031e5b9d853aa7a39a50304356a86afc184b85562b3f228f8197f4d29395c8f  binutils-mips-disable-assert.patch
27ea91e0e406e2ed464fd692cf92a07e338781789f2d968c8b95d9d5545985056a6f7f500df3952e5ab42165db28b741aa33d6b717e880b11a2e41fe406b13c4  binutils-ppc-fix-machine-options.patch
a9efe2689624865f0ff33d4776a5bd295bcad6484bdd38d0ca490fea43691c4933ab33d17478851998eef12922dbf83d6c3225bb1f8faf92a1367d086390f7d3  binutils-s390x-1.patch
0e291df80ad279005265634014d0935d2c115a5ed708d25407094b7ad4ddf267d1fb7fcbcb2d9ad73bd305b4e3974628b820bd1f249f56c095e4896872434cc9  binutils-s390x-2.patch
"
